<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>
    /*
     1.   yield* 可迭代对象    yield的语法糖
    */
    let name = ['小明', '小蓝', '小红']
    let num = [1, 2, 3, 4, 5, 6]

    function* createArrIterator(arr) {
      // for (let i = 0; i < arr.length; i++) {
      //   yield arr[i]
      // }
      yield* arr
    }

    // 2.yield替换类中的实现
    // 之前是要把这个类所创建的所有对象，都编程可迭代对象，用了[Symbol.iterator]实例方法，里边迭代器迭代的是key值
    // 现在我用yield的语法糖可以轻松实现

    class Person {
      constructor(name, age, gender) {
        this.name = name
        this.age = age
        this.gender = gender
      }

      *[Symbol.iterator]() {
        let keys = Object.keys(this)
        yield* keys
      }
    }

    let p1 = new Person('lll', 18, '男')

    // for (let key of p1) {
    //   console.log(key);
    // }

    let piterator = p1[Symbol.iterator]()
    console.log('迭代器是', piterator);

  </script>

</body>

</html>